package com.truyen.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.truyen.model.UploadItem;
import com.truyen.utils.PropertyResolver;

public class UploadImge {
	
	@Autowired
	private PropertyResolver propertyResolver;
	
	//ham upload hinh
	public int uploadImg(int demHinh, int tongHinhCuaTin, Elements lstImage) throws Exception{
		for (int xx = 0; xx < tongHinhCuaTin; xx++) {
			String src = lstImage.get(xx).attr("src");
			String dir = propertyResolver.getValue("link.imagePost");
			String duongDan = dir;
			URL imgSrc = new URL(src);
			URLConnection c = imgSrc.openConnection();
			c.setConnectTimeout(50000);
			c.setReadTimeout(100000);
			InputStream inputStream = imgSrc.openStream();
			demHinh = demHinh+1;
			String fileNameTmp = imgSrc.getFile();
			int a = fileNameTmp.lastIndexOf("/",
					fileNameTmp.length());
			String path = fileNameTmp.substring(0, a);
			dir += path;
			new File(dir).mkdirs();
			fileNameTmp = fileNameTmp.substring(a);
			dir += fileNameTmp;
			
			FileOutputStream outputStream = new FileOutputStream(
					dir);
			int readBytes = 0;
			byte[] buffer = new byte[10000];
			while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
				outputStream.write(buffer, 0, readBytes);
			}
			outputStream.close();
			inputStream.close();
			lstImage.get(xx).attr(
					"src",
					duongDan+path+fileNameTmp);
		}//end for (int xx = 0; xx < tongHinhCuaTin; xx++) {
		return demHinh;
	}
	
	//ham upload hinh
	public String uploadImg(String src) throws Exception{
			String dir = propertyResolver.getValue("link.imagePost");
			String duongDan = dir;
			URL imgSrc = new URL(src);
			URLConnection c = imgSrc.openConnection();
			c.setConnectTimeout(50000);
			c.setReadTimeout(100000);
			InputStream inputStream = imgSrc.openStream();
			String fileNameTmp = imgSrc.getFile();
			int a = fileNameTmp.lastIndexOf("/",
					fileNameTmp.length());
			String path = fileNameTmp.substring(0, a);
			dir += path;
			new File(dir).mkdirs();
			fileNameTmp = fileNameTmp.substring(a);
			dir += fileNameTmp;
			
			FileOutputStream outputStream = new FileOutputStream(
					dir);
			int readBytes = 0;
			byte[] buffer = new byte[10000];
			while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
				outputStream.write(buffer, 0, readBytes);
			}
			outputStream.close();
			inputStream.close();
			return duongDan+path+fileNameTmp;
	}
	
	//upload file 
	public String uploadFile(String dir,UploadItem uploadItem,String tenFileAlias) throws IOException{
		MultipartFile uploadedFile = uploadItem.getFileData();
		InputStream inputStream = uploadedFile.getInputStream();
		String fname = uploadedFile.getOriginalFilename();
		new File(dir).mkdirs();
		dir += "/"+fname;
		FileOutputStream outputStream = new FileOutputStream(dir);
		int readBytes = 0;
		byte[] buffer = new byte[10000];
		while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
			outputStream.write(buffer, 0, readBytes);
		}
		String link = tenFileAlias+"/"+fname;
		outputStream.close();
		inputStream.close();
		return link;
	}
}
